<?php


namespace app\api\controller;


use app\BaseController;
use app\common\service\TenImService;
use app\models\system\Prohibit;
use app\models\user\Users;
use app\Request;
use broadcast\services\UtilService as Util;
use broadcast\redis\setRedis;

class Im extends BaseController
{
    /**
     * 根据用户名称返回用户数据
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUsers(Request $request)
    {
        $param = Util::getMore([
            ['username', '']
            // ['rid', '']
        ]);
        if (!$param['username']) return app("json")->fail("参数不正确!");

        session('website', $request->website());
        $website = session("website");

        if (substr($param['username'], 0, 2) == 'yk') {
            $data = setRedis::getUserCache($website, 'touristList#' . $param['username']);
            if (isset($data['data'])) {
                $data = $data['data'];
            } else {
                $data = [];
            }
        } else {
            $data = setRedis::getUserCache($website, 'usersList#' . $param['username'] . ':*');
            if (!$data) {
                $data = Users::where('username', $param['username'])->field('username,portrait,group,rooms')->findOrEmpty()->toArray();
            }
        }

        if (!empty($data)) {
            // _W_网站标识_R_房间ID_U_用户昵称_X_用户权限
            $rid = $data['rooms'] ?? '1';
            $account = TenImService::getAccount($rid, $data);
            // $account = TenImService::getAccount(1, $data);
            $data['Identifier'] = $account['Identifier'];
            $data['Nick'] = $account['Nick'];
        }
        return app("json")->success($data);
    }

    /**
     * 批量禁言和取消禁言
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     */

    public function forbidSendMsg(Request $request)
    {
        $params = Util::postMore([
            ['GroupId', ''],
            ['Members_Account', []],
            ['ShutUpTime', '']
        ]);

        if (!$params['GroupId'] or empty($params['Members_Account']) or $params['ShutUpTime'] == '') return app("json")->fail("参数不正确!");
        return app("json")->success(TenImService::forbidSendMsg($params));
    }

    /**
     * 获取禁言列表
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     */
    public function getGroupShuttedUin(Request $request)
    {
        $params = Util::postMore([
            ['GroupId', '']
        ]);
        if (!$params['GroupId']) return app("json")->fail("参数不正确!");

        return app("json")->success(TenImService::getGroupShuttedUin($params));
    }

    /**
     * 根据IP 批量禁言和取消禁言
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     */
    public function forbidIp(Request $request)
    {
        $params = Util::postMore([
            ['Ip', []],
            ['username', '']
        ]);
        if (!is_array($params['Ip']) or $params['username'] == '') return app("json")->fail("参数不正确!");
        $username = spliceStr($params['username'], '_U_', '');
        $user = Users::where(['username' => $username])->field('username,group')->find();
        if (!$user or !in_array($user['group'], getAdminsGroup())) {
            return app('json')->fail('权限不足!');
        }
        session('website', $request->website());
        $ips = [];
        foreach ($params['Ip'] as $item) {
            $existence = Prohibit::where("ip",ip2long($item))->count();
            if ($existence) {

            }else{
                $ips[] = ['ip' => ip2long($item), 'create_time' => time(), 'operation' => $user['username']];
            }
            //            $ips['ip'] = ip2long($item);
            //            $item['create_time'] = time();
            //            $item['operation'] = session('admin.username');
        }



        //        $params['Members_Account'] = Users::whereIn('create_ip', $params['Ip'])->select();
        if (!empty($ips)) {
            Prohibit::insertAll($ips);
        }
        return app("json")->success('ok');
        /* if ($params['Members_Account']->isEmpty()) return app('json')->fail('未找到ip用户');
         $params['Members_Account'] = $params['Members_Account']->toArray();
         foreach ($params['Members_Account'] as &$item) {
             $item = TenImService::getAccount(1, $item);
             $item = $item['Identifier'];
         }
         unset($params['ip']);
         return app("json")->success(TenImService::forbidSendMsg($params));*/
    }

    public function getGroupMemberInfo(Request $request)
    {
        $params = Util::getMore([
            ['GroupId', '@TGS#a3ME54ZG5'],
            ['Limit', 300],
            ['Offset', 0]
        ]);

        return app("json")->success(TenImService::getGroupMemberInfo($params));
    }
}
